package com.uroad.lib.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.uroad.lib.R;

public class ComfirmDialog extends Dialog {

    Context mContext;
    View view;
    TextView tvTitle, tvContent;
    Button btnCancel, btnOK;
    DialogOnclick dialogOkClick = null, dialogCancelClick = null;

    public ComfirmDialog(Context context) {
        super(context, R.style.baseCustomDialog_margin20);
        this.mContext = context;
        view = LayoutInflater.from(mContext).inflate(R.layout.dialog_confirm,
                null, false);
        tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        tvContent = (TextView) view.findViewById(R.id.tvContent);
        btnCancel = (Button) view.findViewById(R.id.btnCancel);
        btnOK = (Button) view.findViewById(R.id.btnOK);
        setContentView(view);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
    }

    public void showDialog(String title, String content) {
        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
            tvTitle.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(content)) {
            tvContent.setText(content);
        }
        btnOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (dialogOkClick != null) {
                    dialogOkClick.PerDialogclick(ComfirmDialog.this);
                } else {
                    hideDialog();
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (dialogCancelClick != null) {
                    dialogCancelClick.PerDialogclick(ComfirmDialog.this);
                } else {
                    hideDialog();
                }
            }
        });
        this.show();
    }

    public void setperClick(DialogOnclick okClick, DialogOnclick cancelClick) {
        this.dialogOkClick = okClick;
        if (cancelClick == null) {
            btnCancel.setVisibility(View.GONE);
        }else{
            this.dialogCancelClick = cancelClick;
        }

    }

    public void setButtonText(String cancelText, String okText) {
        if (!TextUtils.isEmpty(cancelText)) {
            btnCancel.setText(cancelText);
        }else{
            btnCancel.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(okText)) {
            btnOK.setText(okText);
        }else{
            btnOK.setVisibility(View.GONE);
        }
    }

    public void hideDialog() {
        this.dismiss();
    }

    public interface DialogOnclick {
        void PerDialogclick(Dialog dialog);
    }

}
